﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MovieRecorder.CApMRCCaptureAreaIndicatorManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Interop;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.MovieRecorder;

public class CApMRCCaptureAreaIndicatorManager
{
  private static CApMRCCaptureAreaIndicatorManager m_instance;
  private CUiMRCRecorderIndicator m_indicatorWindow;
  private CUiMRCRecorderLabelIndicator m_indicatorLabelWindow;
  private CApMRCRecorderSettings m_settings;
  private bool _previewMode;
  private bool _isRecordingPaused;
  private bool _closingMovieSource;

  private CApMRCCaptureAreaIndicatorManager()
  {
    this._previewMode = true;
    this._isRecordingPaused = false;
    this.MovieRecordingViewerSource = (ITxViewerRecordingSource) null;
    this._closingMovieSource = false;
  }

  public static CApMRCCaptureAreaIndicatorManager Instance
  {
    get
    {
      if (CApMRCCaptureAreaIndicatorManager.m_instance == null)
        CApMRCCaptureAreaIndicatorManager.m_instance = new CApMRCCaptureAreaIndicatorManager();
      return CApMRCCaptureAreaIndicatorManager.m_instance;
    }
  }

  public event EventHandler MovieRecordingSourceClosed;

  public ITxViewerRecordingSource MovieRecordingViewerSource { get; private set; }

  private ITxViewerRecordingSource MovieRecordingViewerSourceInit
  {
    get
    {
      if (!(TxApplication.ViewersManager.GetActiveViewer() is ITxViewerRecordingSource viewerSourceInit))
        viewerSourceInit = (ITxViewerRecordingSource) TxApplication.ViewersManager.GraphicViewer;
      return viewerSourceInit;
    }
  }

  public void Update(CApMRCRecorderSettings settings)
  {
    this.m_settings = settings;
    if (settings.ScreenCaptureArea == CaptureArea.Screen)
    {
      this.CloseLabel();
      Rectangle screenCaptureRect = settings.GetScreenCaptureRect();
      this.ShowFrameIndicator(new System.Windows.Point((double) screenCaptureRect.Left, (double) screenCaptureRect.Top), new System.Windows.Point((double) screenCaptureRect.Right, (double) screenCaptureRect.Bottom));
    }
    else
    {
      this.CloseFrameIndicator();
      this.ShowLabel();
    }
  }

  private void ShowFrameIndicator(System.Windows.Point topLeftInPixels, System.Windows.Point bottomRightInPixels)
  {
    if (this.m_indicatorWindow == null)
    {
      this.m_indicatorWindow = new CUiMRCRecorderIndicator(topLeftInPixels, bottomRightInPixels);
      new WindowInteropHelper((Window) this.m_indicatorWindow).Owner = TxApplicationEx.GetMainWindowHandle(IntPtr.Zero);
      this.m_indicatorWindow.Show();
      this.RegisterApplicationEvents();
    }
    else
      this.UpdateFrameIndicator(topLeftInPixels, bottomRightInPixels);
  }

  private void UpdateFrameIndicator(System.Windows.Point topLeftInPixels, System.Windows.Point bottomRightInPixels)
  {
    if (!this.IndicatorWindowIsVisible)
      return;
    this.m_indicatorWindow.UpdateRecordingBorders(topLeftInPixels, bottomRightInPixels);
  }

  public void Close()
  {
    this.CloseFrameIndicator();
    this.CloseLabel();
  }

  private void CloseFrameIndicator()
  {
    if (this.m_indicatorWindow == null)
      return;
    this.UnRegisterApplicationEvents();
    this.m_indicatorWindow.Close();
    this.m_indicatorWindow = (CUiMRCRecorderIndicator) null;
  }

  private void ShowLabel()
  {
    if (this.m_indicatorLabelWindow == null)
    {
      this.m_indicatorLabelWindow = new CUiMRCRecorderLabelIndicator();
      new WindowInteropHelper((Window) this.m_indicatorLabelWindow).Owner = TxApplicationEx.GetMainWindowHandle(IntPtr.Zero);
      this.m_indicatorLabelWindow.Show();
      this.m_indicatorLabelWindow.PreviewMode(this._previewMode);
      this.MovieRecordingViewerSource = (ITxViewerRecordingSource) null;
      this.UpdateLabelIndicationViewerSource();
      TxApplication.ViewersManager.ViewerActivated += new TxViewersManager_ViewerActivatedEventHandler(this.ViewersManager_ViewerActivated);
      TxApplication.ViewersManager.ViewerRemoving += new TxViewersManager_ViewerRemovingEventHandler(this.ViewersManager_ViewerRemoving);
    }
    this.m_indicatorLabelWindow.TimeIndication(this.m_settings.TimeSource, this.m_settings.TimeIntervalAccordingFrameRate);
  }

  private void CloseLabel()
  {
    if (this.m_indicatorLabelWindow == null)
      return;
    this.m_indicatorLabelWindow.Close();
    this.m_indicatorLabelWindow = (CUiMRCRecorderLabelIndicator) null;
    this.CloseLabelIndicationViewerSource();
    TxApplication.ViewersManager.ViewerActivated -= new TxViewersManager_ViewerActivatedEventHandler(this.ViewersManager_ViewerActivated);
    TxApplication.ViewersManager.ViewerRemoving -= new TxViewersManager_ViewerRemovingEventHandler(this.ViewersManager_ViewerRemoving);
  }

  public void StartRecordingMode()
  {
    this._previewMode = false;
    this._isRecordingPaused = false;
    if (this.IndicatorWindowIsVisible)
    {
      this.UnRegisterApplicationEvents();
      this.m_indicatorWindow.ApplyOffset();
    }
    if (this.m_indicatorLabelWindow == null)
      return;
    this.m_indicatorLabelWindow.PreviewMode(this._previewMode);
  }

  private void EnterPreviewMode()
  {
    this._previewMode = true;
    this._isRecordingPaused = false;
    if (this.IndicatorWindowIsVisible)
      this.m_indicatorWindow.RemoveOffset();
    if (this.m_indicatorLabelWindow == null)
      return;
    this.m_indicatorLabelWindow.PreviewMode(this._previewMode);
  }

  public void EndRecordingMode()
  {
    this.EnterPreviewMode();
    if (this.IndicatorWindowIsVisible)
    {
      this.OnApplicationFrameChanged();
      this.RegisterApplicationEvents();
    }
    if (this._closingMovieSource)
      return;
    this.UpdateLabelIndicationViewerSource();
  }

  public void PauseRecordingMode()
  {
    this.EnterPreviewMode();
    this._isRecordingPaused = true;
  }

  private void UpdateLabelIndicationViewerSource()
  {
    if (this.m_indicatorLabelWindow == null || this.MovieRecordingViewerSource == this.MovieRecordingViewerSourceInit)
      return;
    if (this.MovieRecordingViewerSource != null)
      this.CloseLabelIndicationViewerSource();
    this.MovieRecordingViewerSource = this.MovieRecordingViewerSourceInit;
    TxViewersManagerEx.SetViewerFloatingWindow(this.MovieRecordingViewerSource.SourceInstance, (ITxViewerFloatingWindow) this.m_indicatorLabelWindow);
  }

  private void CloseLabelIndicationViewerSource()
  {
    if (this.MovieRecordingViewerSource == null)
      return;
    TxViewersManagerEx.SetViewerFloatingWindow(this.MovieRecordingViewerSource.SourceInstance, (ITxViewerFloatingWindow) null);
    this.MovieRecordingViewerSource = (ITxViewerRecordingSource) null;
  }

  private void ViewersManager_ViewerActivated(
    object sender,
    TxViewersManager_ViewerActivatedEventArgs args)
  {
    if (this.m_indicatorLabelWindow == null || !this._previewMode || this._isRecordingPaused)
      return;
    if (args.Viewer is ITxViewerRecordingSource)
    {
      this.UpdateLabelIndicationViewerSource();
    }
    else
    {
      if (this.MovieRecordingViewerSource != null)
        return;
      this.UpdateLabelIndicationViewerSource();
    }
  }

  private void ViewersManager_ViewerRemoving(
    object sender,
    TxViewersManager_ViewerRemovingEventArgs args)
  {
    if (this.m_indicatorLabelWindow == null || !args.Viewer.Equals((object) this.MovieRecordingViewerSource))
      return;
    this._closingMovieSource = true;
    this.CloseLabelIndicationViewerSource();
    if (this.MovieRecordingSourceClosed != null)
      this.MovieRecordingSourceClosed((object) this, new EventArgs());
    this._closingMovieSource = false;
  }

  private void ApplicationEvents_SizeChanged(object sender, TxApplication_SizeChangedEventArgs args)
  {
    this.OnApplicationFrameChanged();
  }

  private void ApplicationEvents_Moved(object sender, TxApplication_MovedEventArgs args)
  {
    this.OnApplicationFrameChanged();
  }

  private void OnApplicationFrameChanged()
  {
    if (this.m_indicatorWindow == null || !this.m_settings.IsApplicationCaptureMode())
      return;
    Rectangle screenCaptureRect = this.m_settings.GetScreenCaptureRect();
    this.UpdateFrameIndicator(new System.Windows.Point((double) screenCaptureRect.Left, (double) screenCaptureRect.Top), new System.Windows.Point((double) screenCaptureRect.Right, (double) screenCaptureRect.Bottom));
  }

  private bool IndicatorWindowIsVisible
  {
    get => this.m_indicatorWindow != null && this.m_indicatorWindow.IsVisible;
  }

  private void RegisterApplicationEvents()
  {
    TxApplication.ApplicationEvents.Moved += new TxApplication_MovedEventHandler(this.ApplicationEvents_Moved);
    TxApplication.ApplicationEvents.SizeChanged += new TxApplication_SizeChangedEventHandler(this.ApplicationEvents_SizeChanged);
  }

  private void UnRegisterApplicationEvents()
  {
    TxApplication.ApplicationEvents.Moved -= new TxApplication_MovedEventHandler(this.ApplicationEvents_Moved);
    TxApplication.ApplicationEvents.SizeChanged -= new TxApplication_SizeChangedEventHandler(this.ApplicationEvents_SizeChanged);
  }
}
